package actions;

import bin.Mud;
import items.Item;
import items.ItemType;
import items.consumables.Consumable;

public class UseDecider {
    private Mud mudWindow;

    public UseDecider(Mud mudWindow) {
        this.mudWindow = mudWindow;
    }

    public boolean isUse() {
        String command = mudWindow.getCommand();
        return (command.startsWith("USE "));
    }

    public Item getItemToUse() {
        String item = mudWindow.getCommand().replaceFirst("USE ", "");

        for (Item itemToUse : mudWindow.getPlayer().getPlayerInventory(ItemType.CONSUMABLE)) {
            if (item.equalsIgnoreCase(itemToUse.getTitle()) || item.equalsIgnoreCase(itemToUse.getShortName())) {
                return itemToUse;
            }
        }
        return null;
    }
    public boolean isValidUse() {
        return (getItemToUse() != null);
    }

    public void useItem(Consumable itemToUse) {
        itemToUse.useAction(mudWindow.getPlayer());
        mudWindow.getPlayer().removeItemFromInventory(itemToUse);
    }
}
